<template>
  <div class="container">
    <!-- 顶部栏 -->
    <div class="info-box">
      <van-image
        round
        width="64px"
        height="64px"
        :src="userInfo.photo"
        fit="cover"
      />
      <h2>
        {{ userInfo.name }}

        <br />
        <van-tag type="primary" color="#fff" text-color="#009afb">{{
          userInfo.birthday
        }}</van-tag>
      </h2>
    </div>
    <!-- layout布局 -->
    <van-row class="user-nav">
      <van-col span="8">
        <van-icon name="newspaper-o" color="#7af" />我的作品
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#f00" />我的收藏
      </van-col>
      <van-col span="8">
        <van-icon name="notes-o" color="#fa0" />阅读历史
      </van-col>
    </van-row>
    <!-- 编辑列表 -->
    <van-cell-group class="user-group">
      <van-cell
        icon="edit"
        title="编辑资料"
        is-link
        @click="$router.push('/edit')"
      />
      <van-cell icon="chat-o" title="小智同学" is-link />
      <van-cell icon="setting-o" title="系统设置" is-link />
      <van-cell
        icon="warning-o"
        title="退出登录"
        is-link
        @click="logoutClick"
      />
    </van-cell-group>
  </div>
</template>

<script>
/*导入网络中间层 */
import { getUserInfo } from './user.js'
/*导入token工具包*/
import { removeToken } from '@/utils/token.js'
export default {
  name: 'user',
  data () {
    return {
      // 用户信息
      userInfo: {}
    }
  },
  /*生命周期钩子 */
  created () {
    getUserInfo().then(res => {
      console.log(res)
      // 绑定数据
      this.userInfo = res.data.data
      // 将数据更新到vuex
      this.$store.commit('updateData', res.data.data)
    })
  },
  methods: {
    /*退出登录 */
    logoutClick () {
      this.$dialog
        .confirm({
          title: '你确定要退出吗？'
        })
        .then(() => {
          // 用户点击确认执行的回调
          console.log('then')
          // 清除token
          removeToken()
          // 清除用户信息
          this.userInfo = {}
          // 跳转首页
          this.$router.push({ path: '/login' })
        })
        .catch(() => {
          // 用户点击取消执行的回调
          console.log('catch')
        })
    }
  }
}
</script>

<style lang="less">
// 版心
.container {
  .info-box {
    display: flex;
    background-color: #009afb;
    height: 100px;
    color: #fff;
    align-items: center;
    h2 {
      font-size: 14px;
      font-weight: normal;
    }
    .van-image {
      padding: 0px 15px;
    }
  }
}

// 操作导航
.user-nav {
  padding: 15px 0;
  font-size: 12px;
  text-align: center;
  background-color: #fff;
  .van-icon {
    display: block;
    font-size: 24px;
    padding-bottom: 5px;
  }
}
</style>
